package me.nereo.multi_image_selector.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ali.music.multi_image_selector.R;
import com.bumptech.glide.Glide;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import me.nereo.multi_image_selector.bean.Folder;

/**
 * 文件夹Adapter
 * Created by Nereo on 2015/4/7.
 */
public class FolderAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;

    private List<Folder> mFolders = new ArrayList<>();

    private int mImageSize;

    private int mLastSelected = 0;

    /**
     * @param context Context
     */
    public FolderAdapter(Context context) {
        mContext = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mImageSize = mContext.getResources().getDimensionPixelOffset(R.dimen.multi_folder_cover_size);
    }

    /**
     * 设置数据集
     *
     * @param folders List<Folder>
     */
    public void setData(List<Folder> folders) {
        if (folders != null && folders.size() > 0) {
            mFolders = folders;
        } else {
            mFolders.clear();
        }
        notifyDataSetChanged();
    }

    /**
     * @return count
     */
    @Override
    public int getCount() {
        return mFolders.size() + 1;
    }

    /**
     * @param i the mPosition
     * @return the mPosition item
     */
    @Override
    public Folder getItem(int i) {
        if (i == 0) {
            return null;
        }
        return mFolders.get(i - 1);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = mInflater.inflate(R.layout.multi_list_item_folder, viewGroup, false);
            holder = new ViewHolder(view);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        if (holder != null) {
            if (i == 0) {
                holder.mName.setText("所有图片");
                holder.mSize.setText(getTotalImageSize() + "张");
                if (mFolders.size() > 0) {
                    Folder f = mFolders.get(0);
                    Glide.with(mContext)
                            .load(new File(f.mCover.mPath))
                            .error(R.drawable.multi_default_error)
                            .override(mImageSize, mImageSize)
                            .centerCrop()
                            .into(holder.mCover);
//                    Picasso.with(mContext)
//                            .load(new File(f.mCover.mPath))
//                            .error(R.drawable.multi_default_error)
//                            .resize(mImageSize, mImageSize)
//                            .centerCrop()
//                            .into(holder.mCover);
                }
            } else {
                holder.bindData(getItem(i));
            }
        }
        return view;
    }

    private int getTotalImageSize() {
        int result = 0;
        if (mFolders != null && mFolders.size() > 0) {
            for (Folder f : mFolders) {
                result += f.mImages.size();
            }
        }
        return result;
    }

    /**
     * the select mPosition
     *
     * @param i mPosition
     */
    public void setSelectIndex(int i) {
        if (mLastSelected == i) {
            return;
        }

        mLastSelected = i;
        notifyDataSetChanged();
    }

    /**
     * @return the select mPosition
     */
    public int getSelectIndex() {
        return mLastSelected;
    }

    class ViewHolder {
        private ImageView mCover;
        private TextView mName;
        private TextView mSize;
        private ImageView mIndicator;

        ViewHolder(View view) {
            mCover = (ImageView) view.findViewById(R.id.cover);
            mName = (TextView) view.findViewById(R.id.name);
            mSize = (TextView) view.findViewById(R.id.size);
            mIndicator = (ImageView) view.findViewById(R.id.indicator);
            view.setTag(this);
        }

        void bindData(Folder data) {
            mName.setText(data.mName);
            mSize.setText(data.mImages.size() + "张");
            // 显示图片
            Glide.with(mContext)
                    .load(new File(data.mCover.mPath))
//                    .placeholder(R.drawable.multi_default_error)
                    .error(R.drawable.multi_default_error)
                    .override(mImageSize, mImageSize)
                    .centerCrop()
                    .into(mCover);
//            Picasso.with(mContext)
//                    .load(new File(mData.mCover.mPath))
//                    .placeholder(R.drawable.multi_default_error)
//                    .resize(mImageSize, mImageSize)
//                    .centerCrop()
//                    .into(mCover);
        }
    }

}
